/**
 * Created by alice on 2017/2/17 0017
 */
;-function (window) {
    "use strict";
    //TODO 预设变量写这里，全局变量写在base.js 里面 使用 r. 调用
    var ready = {
        layer: null,
        city: r.court_id,
        page_size: 12,
        modal_page_size: 12
    };
    //TODO 初始化的方法写这里面
    var init = function () {
        init_ready();//加载预设变量
        func.select_courts();
        func.all_courtroom_judge_secretary(r.court_id);
        func.count_major();
        func.init();
        init_event();
        function init_event() {//TODO 初始化页面事件
        }

        function init_ready() {//TODO 初始化预设值
            layui.use('layer', function () {
                ready.layer = layui.layer;
            });
        }
    };
    //TODO 内部调用的方法写这里
    var func = {
        init: function () {
            $('#startTime').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                console.log(ev.date.valueOf());
            });

            $('#endTime').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                console.log(ev.date.valueOf());
            });
            // $("#endTime").find("input").val(currentDay);

            $('#modalstartTime').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                console.log(ev.date.valueOf());
            });

            $('#modalendTime').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                console.log(ev.date.valueOf());
            });

            $("#selectCourts").change(function () {
                func.all_child_option_change();
            });
            $("#modalselectCourts").change(function () {
                func.modal_all_child_option_change();
            });
        },
        count_major: function () {
            var select_court = $('#selectCourts').val();
            var select_court_room = $('#allCourtRooms').val();
            var case_type = $('#caseTypeName').val();
            var case_no = $('#caseno').val();
            var case_name = $('#caseName').val();
            var select_judge = $('#allJudges').val();
            var select_secretary = $('#allSecretary').val();
            var start_time = $("#startTime").find("input").val();
            var end_time = $("#endTime").find("input").val();
            var status = $("#status").val();
            // console.log(select_court, select_court_room, case_type, case_no, case_name, select_judge);
            $.post(r.base_path + "major/findSumNumMajor", {
                courtId: select_court,
                courtRoom: select_court_room,
                caseTypeName: case_type,
                caseno: case_no,
                caseName: case_name,
                judge: select_judge,
                secretary: select_secretary,
                startTime: start_time,
                endTime: end_time,
                status: status
            }, function (result) {
                var s = result.data;
                if (!s) {
                    $(".row-no-padding").html('<span>暂无大案要案</span>');
                    $("#loading").hide();
                    func.page(1, 1, null);
                } else {
                    func.page(1, Math.ceil(s / ready.page_size), func.select_major);
                    func.select_major(1);
                }
            }, 'json');
        },
        select_major: function (page_no) {
            var select_court = $('#selectCourts').val();
            var select_court_room = $('#allCourtRooms').val();
            var case_type = $('#caseTypeName').val();
            var case_no = $('#caseno').val();
            var case_name = $('#caseName').val();
            var select_judge = $('#allJudges').val();
            var select_secretary = $('#allSecretary').val();
            var start_time = $("#startTime").find("input").val();
            var end_time = $("#endTime").find("input").val();
            var status = $("#status").val();
            $.get(r.base_path + "major/findMajorInfo", {
                courtId: select_court,
                courtRoom: select_court_room,
                caseTypeName: case_type,
                caseno: case_no,
                caseName: case_name,
                judge: select_judge,
                secretary: select_secretary,
                startTime: start_time,
                endTime: end_time,
                status: status,
                page: page_no,
                pageSize: ready.page_size
            }, function (result) {
                var row_no_padding = $(".row-no-padding").empty();
                var flag = 1;//表示今日
                if (result.status == 0) {
                    if (result.data.length == 0) {
                        row_no_padding.append('<span>暂无开庭信息</span>');
                    } else {
                        var picNum = 6;
                        $.each(result.data, function (i, valueOfElement) {
                            var caseName = valueOfElement.casename;
                            var startDate = valueOfElement.start_date;
                            var uid = valueOfElement.uid;
                            if (startDate) {
                                var date = new Date(startDate);
                                startDate = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " +
                                    date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
                            }
                            var status = valueOfElement.status;
                            var courtroom_name = valueOfElement.courtroom_name;
                            var cid = valueOfElement.courtroom_id;
                            var statusName = "";
                            if (0 == status) {
                                statusName = "排期";
                            } else if (1 == status) {
                                statusName = "开庭";
                            } else if (2 == status) {
                                statusName = "休庭";
                            } else if (3 == status) {
                                statusName = "闭庭";
                            } else {
                                statusName = "归档";
                            }
                            var randomNum = parseInt(picNum * Math.random() + 1);//随机出1到picNum的整数
                            var href = r.base_path + "views/system_monitor/play.html?planId=" + uid + "&vc=" + valueOfElement.courtId+'&court=' + r.court_id;
                            if (valueOfElement.status < 3) {
                                href = r.base_path + "views/system_monitor/living.html?uid=" + uid + "&cid=" + cid + "&flag=" + flag + "&city=" + ready.city + "&vc=" + valueOfElement.courtId+'&court=' + r.court_id;
                            }
                            var html = '<div class="col-md-2 col-xs-6">' +
                                '<div class="more-major-item">' +
                                '<a href="' + href + '">' +
                                '<div class="more-major-item-img-panel">' +
                                '<p class="more-major-item-img-panel-top" >' + (courtroom_name ? courtroom_name : "未知") + '</p>' +
                                '<img src="' + r.base_path + "assets/img/more_court_test" + randomNum + ".jpg" + '" alt="法院" class="img">' +
                                '<p class="more-major-item-img-panel-bottom">' + statusName + '</p>' +
                                '</div>' +
                                '<p id="startDate">' + startDate + '</p>' +
                                '<p id="caseNoDis">' + (valueOfElement.caseno ? valueOfElement.caseno : "&nbsp;") + '</p>' +
                                '</a>' +
                                '<p class="more-major-item-depiction">' +
                                '<span id="_court_courtroom">' + (caseName ? caseName : "&nbsp;") + '</span>' +
                                '</p>' +
                                '</div>' +
                                '</div>';
                            row_no_padding.append(html);
                        });
                    }
                    row_no_padding.show();
                }
                $("#loading").hide();
            }, 'json').fail(function () {
                $("#loading").hide();
            });
        },
        select_courts: function () {
            // $.ajax({
            //     url: r.base_path + "index/initNavBar",
            //     type: "get",
            //     data: {
            //         courtId: r.user_court_id
            //     },
            //     dataType: "json",
            //     success: function (result) {
            //         if (result.data) {
            //
            //             $.each(result.data, function (i, n) {
            //                 select_court.append("<option value='" + n.courtid + "'>" + n.courtname + "</option>");
            //                 modal_select_court.append("<option value='" + n.courtid + "'>" + n.courtname + "</option>");
            //             });
            //         }
            //     }
            // });
            var select_court = $("#selectCourts").empty();
            select_court.append("<option value='' selected>全部法院</option>");
            var modal_select_court = $("#modalselectCourts").empty();
            modal_select_court.append("<option value='' selected>全部法院</option>");
            $.each(r.court_list, function (i, n) {
                select_court.append("<option value='" + n.courtid + "'>" + n.courtname + "</option>");
                modal_select_court.append("<option value='" + n.courtid + "'>" + n.courtname + "</option>");
            })
        },
        modal_count_major: function () {
            $('#loading').show();
            var select_court = $('#modalselectCourts').val();
            var select_court_room = $('#modalallCourtRooms').val();
            var case_type = $('#modalcaseTypeName').val();
            var case_no = $('#modalcaseno').val();
            var case_name = $('#modalcaseName').val();
            var select_judge = $('#modalallJudges').val();
            var select_secretary = $('#modalallSecretary').val();
            var start_time = $("#modalstartTime").find("input").val();
            var end_time = $("#modalendTime").find("input").val();
            var modalstatus = $("#modalstatus").val();
            // console.log(select_court, select_court_room, case_type, case_no, case_name, select_judge);
            $.get(r.base_path + "trial/findSumNumCase", {
                courtId: select_court,
                courtRoom: select_court_room,
                caseTypeName: case_type,
                caseno: case_no,
                caseName: case_name,
                judge: select_judge,
                secretary: select_secretary,
                startTime: start_time,
                endTime: end_time,
                status: modalstatus
            }, function (result) {
                var s = result.data;
                if (s == 0) {
                    $("#table").html('<span>暂无开庭信息</span>');
                    $("#loading").hide();
                    func.modal_page(1, 1, null);
                } else {
                    func.modal_page(1, Math.ceil(s / ready.modal_page_size), func.modal_select_major);
                    func.modal_select_major(1);
                }
            },'json').fail(function () {
                $('#loading').hide();
            });
        },
        modal_select_major: function (page_no) {
            $('#loading').show();
            var select_court = $('#modalselectCourts').val();
            var select_court_room = $('#modalallCourtRooms').val();
            var case_type = $('#modalcaseTypeName').val();
            var case_no = $('#modalcaseno').val();
            var case_name = $('#modalcaseName').val();
            var select_judge = $('#modalallJudges').val();
            var select_secretary = $('#modalallSecretary').val();
            var start_time = $("#modalstartTime").find("input").val();
            var end_time = $("#modalendTime").find("input").val();
            var modalstatus = $("#modalstatus").val();
            $.get(r.base_path + "trial/getCaseByCondition", {
                courtId: select_court,
                courtRoom: select_court_room,
                caseTypeName: case_type,
                caseno: case_no,
                caseName: case_name,
                judge: select_judge,
                secretary: select_secretary,
                startTime: start_time,
                endTime: end_time,
                page: page_no,
                pageSize: ready.modal_page_size,
                status: modalstatus
            }, function (result) {
                if (result.status == 0) {
                    var table = $('#table');
                    if (result.data.length == 0) {
                        table.html('<span>暂无开庭信息</span>');
                    } else {
                        var data = result.data;
                        $.each(data, function (i, valueOfElement) {
                            data[i].courtId = valueOfElement.courtId;
                            var startDate = valueOfElement.start_date;
                            if (startDate) {
                                var date = new Date(startDate);
                                data[i].startDate = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDay() + " " +
                                    date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
                            }
                            var status = valueOfElement.status;
                            if (0 == status) {
                                data[i].status = "排期";
                            } else if (1 == status) {
                                data[i].status = "开庭";
                            } else if (2 == status) {
                                data[i].status = "休庭";
                            } else if (3 == status) {
                                data[i].status = "闭庭";
                            } else {
                                data[i].status = "归档";
                            }
                        });
                        table.bootstrapTable("load", data);
                        table.show();
                    }
                }
                $('#loading').hide();
            }, 'json').fail(function () {
                $('#loading').hide();
            });
        },
        init_table: function () {
            $('#loading').show();
            var table = $('#table');
            func.modal_all_courtroom_judge_secretary();
            table.bootstrapTable({
                columns: [{
                    field: 'Number',
                    title: '序号',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                }, {
                    field: 'caseno',
                    title: '案号'
                }, {
                    field: 'casename',
                    title: '案件名称',
                    width: "40%"
                }, {
                    field: 'status',
                    title: '审理状态'
                }, {
                    field: 'edit',
                    title: '操作',
                    sortable: false,
                    formatter: function (value, row) {
                        // console.log(row.uid);
                        // // '<a href="javascript:deleteCourtequipment(\'' + index + '\')" ><i class="fa fa-trash-o fa-lg"></i></a>';
                        return '<a href="javascript:p._detail(\'' + row.uid + '\',\'' + row.courtId + '\')"><i class="fa fa-file-text fa-lg"></i></a>&nbsp;' +
                            '<a href="javascript:p._add(\'' + row.uid + '\',\'' + row.courtId + '\')"><i class="fa fa-plus-circle fa-lg"></i></a>&nbsp;';
                    }
                }],
                showFooter: false
            });
            table.bootstrapTable("hideLoading");
            func.modal_count_major();
        },
        _detail: function (uid, detail_court_id) {
            $('#_detail').modal("show");
            $.ajax({
                url: r.base_path + "getCaseInfoByUid",
                type: "post",
                dataType: "json",
                data: {
                    uid: uid,
                    courtId: detail_court_id
                },
                success: function (result) {
                    var caseInfo = result.data;
                    var caseNo = caseInfo.caseno;
                    var caseName = caseInfo.casename;
                    var subName = "";
                    var caseTypeName = caseInfo.casetype_name;
                    var judgeName = caseInfo.judge_id;
                    var secretaryName = caseInfo.secretary_id;
                    var courtroomName = caseInfo.courtroom_name;
                    var statusName = caseInfo.statusName;
                    var parties = caseInfo.accuse + " " + caseInfo.accused;
                    var startDate = caseInfo.start_date;
                    startDate = startDate.substring(0, startDate.length - 2);
                    var caseDesc = caseInfo.casedesc;
                    if (caseDesc == null) {
                        caseDesc = "";
                    }
                    var judge_name = caseInfo.judge_name ? caseInfo.judge_name : '&nbsp;';
                    var secretary_name = caseInfo.secretary_name ? caseInfo.secretary_name : '&nbsp;';
                    console.log(result);
                    var html = ['<div>',
                        '                        <div class="row">',
                        '                            <div class="col-md-3 col-xs-4 text-right">',
                        '                                <span> 案  号：</span>',
                        '                            </div>',
                        '                            <div class="col-md-3 col-xs-8 text-left">',
                        '                                <span>' + caseNo + '</span>',
                        '                            </div>',
                        '                        </div>',
                        '                        <div class="row row-court">',
                        '                            <div class="col-md-3 col-xs-4 text-right">',
                        '                                <span>案件类型：</span>',
                        '                            </div>',
                        '                            <div class="col-md-3 col-xs-8 text-left">',
                        '                                <span>' + caseTypeName + '</span>',
                        '                            </div>',
                        '                        </div>',
                        '                        <div class="row row-court">',
                        '                            <div class="col-md-3 col-xs-4 text-right">',
                        '                                <span>案件名称：</span>',
                        '                            </div>',
                        '                            <div class="col-md-9 col-xs-8 text-left">',
                        '                                <span>' + caseName + '</span>',
                        '                            </div>',
                        '                        </div>',
                        '                        <div class="row row-court">',
                        '                            <div class="col-md-3 col-xs-4 text-right">',
                        '                                <span> 审判长：</span>',
                        '                            </div>',
                        '                            <div class="col-md-3  col-xs-8 text-left">',
                        '                                <span>' + judge_name + '</span>',
                        '                            </div>',
                        '                        </div>',
                        '                        <div class="row row-court">',
                        '                              <div class="col-md-3 col-xs-4 text-right">',
                        '                                <span> 书记员：</span>',
                        '                            </div>',
                        '                            <div class="col-md-3  col-xs-8 text-left">',
                        '                                <span>' + secretary_name + '</span>',
                        '                            </div>',
                        '                        </div>',
                        '                        <div class="row row-court">',
                        '                            <div class="col-md-3 col-xs-4 text-right">',
                        '                                <span>当事人：</span>',
                        '                            </div>',
                        '                            <div class="col-md-9 col-xs-8 text-left">',
                        '                                <span>' + parties + '</span>',
                        '                            </div>',
                        '                        </div>',
                        '                        <div class="row row-court">',
                        '                            <div class="col-md-3 col-xs-4 text-right">',
                        '                                <span> 开庭地点：</span>',
                        '                            </div>',
                        '                            <div class="col-md-9 col-xs-8 text-left">',
                        '                                <span>' + courtroomName + '</span>',
                        '                            </div>',
                        '                        </div>',
                        '                        <div class="row row-court">',
                        '                            <div class="col-md-3 col-xs-4 text-right">',
                        '                                <span>开庭时间：</span>',
                        '                            </div>',
                        '                            <div class="col-md-3 col-xs-8 text-left">',
                        '                                <span>' + startDate + '</span>',
                        '                            </div>',
                        '                            <div class="col-md-3 col-xs-4 text-right">',
                        '                                <span> 庭审状态:</span>',
                        '                            </div>',
                        '                            <div class="col-md-3 col-xs-8 text-left">',
                        '                                <span>' + statusName + '</span>',
                        '                            </div>',
                        '                        </div>',
                        '                        <div class="row row-court">',
                        '                            <div class="col-md-3 col-xs-4 text-right">',
                        '                                <span>   基本案情：</span>',
                        '                            </div>',
                        '                            <div class="col-md-9 col-xs-8 text-left">',
                        '                                <span>' + caseDesc + '</span>',
                        '                            </div>',
                        '                        </div>',
                        '                    </div>'].join("");
                    $("#_court_info").html(html);
                },
                error: function () {
                    $("#loading").hide();
                }
            });
        },
        _add: function (uid, court) {
            layui.use(['layer', 'form'], function () {
                var layer = layui.layer;
                layer.confirm('想要设置该案为大案要案吗?', {
                    btn: ['是的', '取消'] //按钮
                }, function () {
                    $.post(r.base_path + "major/insertMajorInfo", {planId: uid, courtId: court}, function (result) {
                        layer.msg(result.msg);
                    },'json')
                }, function () {

                });
            });
        },
        all_child_option_change: function () {
            var current_court_id = $("#selectCourts").val();
            func.all_courtroom_judge_secretary(current_court_id);
        },
        all_courtroom_judge_secretary: function (current_court_id) {
            if (current_court_id) {
                ready.city = current_court_id;
            }
            $.get(r.base_path + "trial/selectCourtAndJudgeAndSecretary", {courtId: ready.city}, function (result) {
                if (result.data) {
                    var all_room = $("#allCourtRooms").empty();
                    all_room.append("<option value=''>" + "所有法庭" + "</option>");
                    $.each(result.data.court, function (i, valueOfElement) {
                        all_room.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.courtroomname + "</option>");
                    });
                    var case_type = $("#caseTypeName").empty();
                    case_type.append("<option value=''>" + "所有类型" + "</option>");
                    $.each(result.data.caseType, function (i, valueOfElement) {
                        case_type.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                    });
                    var all_judge = $("#allJudges").empty();
                    all_judge.append("<option value=''>" + "所有法官" + "</option>");
                    $.each(result.data.judges, function (i, valueOfElement) {
                        all_judge.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.username + "</option>");
                    });
                    var all_secretary = $("#allSecretary").empty();
                    all_secretary.append("<option value=''>" + "所有书记员" + "</option>");
                    $.each(result.data.secretaries, function (i, valueOfElement) {
                        all_secretary.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.username + "</option>");
                    });
                }
            }, 'json');
        },
        modal_all_child_option_change: function () {
            var current_court_id = $("#modalselectCourts").val();
            func.modal_all_courtroom_judge_secretary(current_court_id);
        },
        modal_all_courtroom_judge_secretary: function (current_court_id) {
            if (current_court_id) {
                ready.city = current_court_id;
            }
            $.post(r.base_path + "trial/selectCourtAndJudgeAndSecretary", {courtId: ready.city}, function (result) {
                if (result.data) {
                    var modal_all_room = $("#modalallCourtRooms").empty();
                    modal_all_room.append("<option value=''>" + "所有法庭" + "</option>");
                    $.each(result.data.court, function (i, valueOfElement) {
                        modal_all_room.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.courtroomname + "</option>");
                    });
                    var modal_case_type = $("#modalcaseTypeName").empty();
                    modal_case_type.append("<option value=''>" + "所有类型" + "</option>");
                    $.each(result.data.caseType, function (i, valueOfElement) {
                        modal_case_type.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                    });
                    var modal_all_judge = $("#modalallJudges").empty();
                    modal_all_judge.append("<option value=''>" + "所有法官" + "</option>");
                    $.each(result.data.judges, function (i, valueOfElement) {
                        modal_all_judge.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.username + "</option>");
                    });
                    var modal_all_secretary = $("#modalallSecretary").empty();
                    modal_all_secretary.append("<option value=''>" + "所有书记员" + "</option>");
                    $.each(result.data.secretaries, function (i, valueOfElement) {
                        modal_all_secretary.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.username + "</option>");
                    });
                }
            }, 'json');
        },
        page: function (now, totalPages, callback) {
            base.pagination('#_page', {
                currentPage: now,
                totalPages: totalPages,
                onPageClicked: function (page) {
                    callback(page);
                }
            });
        },
        modal_page: function (now, totalPages, callback) {
            base.pagination('#_modal_page', {
                currentPage: now,
                totalPages: totalPages,
                onPageClicked: function (page) {
                    callback(page);
                }
            });
        }
    };
    init();
    //TODO 对外公开的方法写在这里
    window.p = {
        _detail: func._detail,
        _add: func._add,
        count_major: func.count_major,
        modal_count_major: func.modal_count_major,
        init_table: func.init_table
    };
}(window);